package com.feige.config;


import com.feige.filter.JwtFilter;
import com.feige.handler.TokenLogoutHandler;
import com.feige.handler.UnauthorizedEntryPoint;
import com.feige.utils.jwt.JwtUtils;
import com.feige.utils.redis.RedisUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

/**
 * springSecurity 配置类
 * @author feige
 */
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class TokenWebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private UserDetailsService userDetailsService;

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private JwtUtils jwtUtils;

    /**
     * 解决无法注入AuthenticationManager
     * @return
     * @throws Exception
     */
    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @SuppressWarnings("all")
    @Bean
    public BCryptPasswordEncoder getBCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }


    /**
     * 配置设置
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.exceptionHandling()
                .authenticationEntryPoint(new UnauthorizedEntryPoint())
                //禁用csrf
                .and().cors()
                .and().csrf().disable()
                //允许基于使用HttpServletRequest限制访问
                .authorizeRequests()
                .anyRequest().authenticated()
                //.anyRequest().permitAll()
                //指定登出URL
                .and().logout().logoutUrl("/auth/logout").permitAll()
                .logoutSuccessHandler(new TokenLogoutHandler(redisUtils,jwtUtils))
                .and()
                .addFilterBefore(new JwtFilter(redisUtils,jwtUtils), UsernamePasswordAuthenticationFilter.class)
                .httpBasic();

    }

    /**
     * 密码处理
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService)
                .passwordEncoder(getBCryptPasswordEncoder());
    }

    /**
     * 配置那些请求不拦截
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers(
                "/auth/login",
                "/auth/captcha_image",
                "/auth/sender/**",
                "/auth/validate/**",
                "/auth/register"
        );
    }
}
